<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends Controller{


	//商品种类添加
	public function cateadd(){
		if(IS_POST){
			$cateModel = D('cate');
			$data = $cateModel->create();
			$data['cat_recommend'] = implode(',',$_POST['cat_recommend']);
			$cateModel->add($data);
			$this->redirect('Cate/catelist');
		} else {
			$cateModel = D('cate');
			$data = $cateModel->catelist();
			$this->assign('vl',$data);
			$this->display();
		}		
	}

    //商品种类修改
	public function cateedit(){
		if(IS_POST){
			$id = $_GET['cat_id'];
			$cateModel = D('cate');
			$data = $cateModel -> create();
			$cateModel->where("cat_id=$id")->save($data); 
		    $this->redirect('Cate/catelist');
		} else {
			$id = $_GET['cat_id'];
			$cateModel = D('cate');
			$data = $cateModel->where("cat_id=$id")->select();
			//将$data[0]['cat_recommend'] 的值转换为一个一维数组
			$data[0]['cat_recommend'] = explode(',',$data[0]['cat_recommend']);
			$vl = $cateModel->catelist();//生成下拉列表
			$this->assign('vl',$vl);
			$this->assign('value',$data);
			$this->display('cateedit');
		}		
	}


	//商品种类删除
	public function catedel(){
		$id = $_GET['cat_id'];
		$cateModel = D('cate');
		$vl= $cateModel->where("cat_id=$id")->delete();
		$this->assign('vl',$vl);
		$this->redirect('Cate/catelist');
	}

	//商品种类列表
	public function catelist(){
		$cateModel = D('cate');
		$vl = $cateModel->catelist();
		// var_dump($vl);
		$this->assign('vl',$vl);
		$this->display('catelist');
	}
}